package cc.admin.modules.wea.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.modules.sys.entity.SysCascade;
import cc.admin.modules.sys.service.ISysCascadeService;
import cc.admin.modules.wea.entity.WeaPredictRoute;
import cc.admin.modules.wea.entity.WeaPredictRouteDetail;
import cc.admin.modules.wea.service.IWeaPredictRouteDetailService;
import cc.admin.modules.wea.service.IWeaPredictRouteService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 航线预测表
 * @Author: cc-admin
 * @Date:   2024-08-30
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags="航线预测表")
@RestController
@RequestMapping("/wea/predictRoute")
public class WeaPredictRouteController extends BaseController<WeaPredictRoute, IWeaPredictRouteService> {
	@Autowired
	private IWeaPredictRouteService weaPredictRouteService;
	@Autowired
  private IWeaPredictRouteDetailService weaPredictRouteDetailService;

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "航线预测表-分页列表查询")
	@ApiOperation(value="航线预测表-分页列表查询", notes="航线预测表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
								   @RequestParam(name="key",required = false) String key,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WeaPredictRoute> queryWrapper = QueryGenerator.initQueryWrapper(new WeaPredictRoute(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {

		}

		Page<WeaPredictRoute> page = new Page<WeaPredictRoute>(pageNo, pageSize);
		IPage<WeaPredictRoute> pageList = weaPredictRouteService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param weaPredictRoute
	 * @return
	 */
	@AutoLog(value = "航线预测表-添加")
	@ApiOperation(value="航线预测表-添加", notes="航线预测表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WeaPredictRoute weaPredictRoute) {
        weaPredictRoute.setId(IdUtil.objectId());
        weaPredictRouteService.save(weaPredictRoute);
        if(weaPredictRoute.getWeaPredictRouteDetailList().size()>0){
          weaPredictRoute.getWeaPredictRouteDetailList().forEach(item->{
            WeaPredictRouteDetail weaPredictRouteDetail = new WeaPredictRouteDetail();
            weaPredictRouteDetail.setParentId(weaPredictRoute.getId());
            weaPredictRouteDetail.setLat(item.getLat());
            weaPredictRouteDetail.setLon(item.getLon());
            weaPredictRouteDetail.setRecordTime(item.getRecordTime());
            weaPredictRouteDetail.setTenantId(item.getTenantId());
            weaPredictRouteDetailService.save(weaPredictRouteDetail);
          });
        }
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param weaPredictRoute
	 * @return
	 */
	@AutoLog(value = "航线预测表-编辑")
	@ApiOperation(value="航线预测表-编辑", notes="航线预测表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WeaPredictRoute weaPredictRoute) {
        weaPredictRouteService.updateById(weaPredictRoute);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", weaPredictRoute.getId());
        weaPredictRouteDetailService.remove(queryWrapper);
    if(weaPredictRoute.getWeaPredictRouteDetailList().size()>0){
      weaPredictRoute.getWeaPredictRouteDetailList().forEach(item->{
        WeaPredictRouteDetail weaPredictRouteDetail = new WeaPredictRouteDetail();
        weaPredictRouteDetail.setParentId(weaPredictRoute.getId());
        weaPredictRouteDetail.setLat(item.getLat());
        weaPredictRouteDetail.setLon(item.getLon());
        weaPredictRouteDetail.setRecordTime(item.getRecordTime());
        weaPredictRouteDetail.setTenantId(item.getTenantId());
        weaPredictRouteDetailService.save(weaPredictRouteDetail);
      });
    }

		return Result.ok("编辑成功!");
	}


	/**
	 *   复制
	 * @param id
	 * @param name
	 * @return
	 */
	@AutoLog(value = "航线预测表-复制")
	@ApiOperation(value="航线预测表-复制", notes="航线预测表-复制")
	@PostMapping(value = "/copy")
	public Result<?> copy(@RequestParam(name="id",required=true) String id,@RequestParam(name="name",required=true) String name) {
        WeaPredictRoute weaPredictRoute = weaPredictRouteService.getById(id);
        weaPredictRoute.setId(IdUtil.fastUUID());
        weaPredictRoute.setName(name);
        weaPredictRoute.setCreateTime(new Date());
        weaPredictRouteService.save(weaPredictRoute);
		return Result.ok("复制成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "航线预测表-通过id删除")
	@ApiOperation(value="航线预测表-通过id删除", notes="航线预测表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        weaPredictRouteService.removeById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",id);
        weaPredictRouteDetailService.remove(queryWrapper);
    return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "航线预测表-批量删除")
	@ApiOperation(value="航线预测表-批量删除", notes="航线预测表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.weaPredictRouteService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "航线预测表-通过id查询")
	@ApiOperation(value="航线预测表-通过id查询", notes="航线预测表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        WeaPredictRoute weaPredictRoute = weaPredictRouteService.getById(id);
        QueryWrapper queryWrapper= new QueryWrapper();
        queryWrapper.eq("parent_id",id);
        List<WeaPredictRouteDetail> list=weaPredictRouteDetailService.list(queryWrapper);
        weaPredictRoute.setWeaPredictRouteDetailList(list);
		return Result.ok(weaPredictRoute);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param weaPredictRoute
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, WeaPredictRoute weaPredictRoute) {
      return super.exportXls(request, weaPredictRoute, WeaPredictRoute.class, "航线预测表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, WeaPredictRoute.class);
  }
  @RequestMapping(value = "/sevenDayWeather")
  public Result<?> SevenDayWeather(@RequestParam(name="id",required=true) String id){
   List<Map<String,Object>> mapList=  weaPredictRouteService.sevenDayWeather(id);
    return Result.ok(mapList);
  }



}
